//修改密码的控制层
angular.module('revisepasswordcontrollers', [])
.controller('MeRevisePasswordCtrl', function ($scope,$ionicActionSheet,$state,$timeout,Storage,MeRevisePassword,Toast,Loading) {

    var memberId = Storage.get("member").member_id;

    $scope.countInfo = {
      member_id : 0,
      lastpwd :"",
      newpwd :"",
      confirmpwd : ""
    }



    //保存修改密码
    $scope.savePwd = function () {
      console.log($scope.countInfo);
      //1 判断输入的内容有没有空值
      if($scope.countInfo.newpwd =="" || $scope.countInfo.confirmpwd=="" || $scope.countInfo.lastpwd==""){
        Toast.showMessage("输入的内容不能为空");
        /*$ionicLoading.show({
          noBackdrop: true,
          template: '输入的内容不能为空',
          duration: 1500
        });*/

      }
      //2 判断两次输入密码是否相同
      else if($scope.countInfo.newpwd != $scope.countInfo.confirmpwd){
        Toast.showMessage("两次输入的密码不一致");
       /* $ionicLoading.show({
          noBackdrop: true,
          template: '两次输入的密码不一致',
          duration: 1500
        });*/
      }else{
        //3 判断旧密码是否匹配
        $scope.countInfo.member_id = memberId;
        //向后台请求数据（请求密码）
        MeRevisePassword.getPwd($scope.countInfo);
      }
    }

    //访问后台成功，处理结果
    $scope.$on('MeRevise.getPwdResult', function () {
      var result = MeRevisePassword.getPwdJson();
      if(result.editpwdStatus){
        //密码修改成功
        Toast.showMessage("密码修改成功");
       /* $ionicLoading.show({
          noBackdrop: true,
          template: "密码修改成功",
          duration: 1500
        });*/
        $timeout(function () {
          $state.go('my-data');
        }, 2000);

      }else{
        //密码修改失败
        Toast.showMessage(result.editpwdMsg);
        /*$ionicLoading.show({
          noBackdrop: true,
          template: result.editpwdMsg,
          duration: 1500
        });*/
        //清空输入框
        $scope.countInfo.lastpwd = "";
        $scope.countInfo.newpwd = "";
        $scope.countInfo.confirmpwd = "";
      }
    })




  })
